#include <iostream>
#include <vector>

using namespace std;
class Solution
{
public:
   vector<vector<int>> largeGroupPositions(string s)
   {
      vector<vector<int>> ret;
      int n = s.size();
      int num = 1;
      for (int i = 0; i < n; i++)
      {
         if (i == n - 1 || s[i] != s[i + 1])
         {
            if (num >= 3)
            {
               ret.push_back({i - num + 1, i});
            }
            num = 1;
         }
         else
         {
            num++;
         }
      }
      return ret;
   }

   vector<vector<int>> largeGroupPositions_2(string s)
   {
      vector<vector<int>> ans;
      // 类似滑动窗口机制
      int i = 0, j = 0;
      while (i < s.size())
      {
         // 若前后相等，则j指针向后滑动
         if (j < s.size() && s[i] == s[j])
         {
            j++;
         }
         // 否则，先判断长度
         else
         {
            // 长度>=3
            if (j - i >= 3)
            {
               ans.push_back({i, j - 1});
            }
            // 窗口i向后滑动到j的位置
            i = j;
            // 窗口J向后滑动一个单位
            j++;
         }
      }
      return ans;
   }
};
int main()
{
   Solution s;
   vector<vector<int>> ans = s.largeGroupPositions("abcdddeeeeaabbbcd");
   for (auto &a : ans)
   {
      cout << a[0] << " " << a[1];
      cout << endl;
   }
   cout << endl;
   system("pause");
   return 0;
}
